package com.ag.lzm.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.springframework.stereotype.Service;

@Service("LZMFileUtil")
public class FileUtil {
	/*
	 * 复制 源/目标文件路径或文件夹路径作为参数
	 */

	public void copyFileOrDir(String sourceURL, String targetURL) {
		File source = new File(sourceURL);
		if (!source.exists()) {
			System.out.println("源文件不存在");
			return;
		}
		System.out.println("ksjdf");
		File target = new File(targetURL);
		FileInputStream fis = null;
		FileOutputStream fos = null;
		if (source.isFile()) {
			try {
				fis = new FileInputStream(sourceURL);
				fos = new FileOutputStream(targetURL);
				byte[] buff = new byte[1024];
				int readed = -1;
				while ((readed = fis.read(buff)) > 0)
					fos.write(buff, 0, readed); // 复制
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				fis.close();
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else if (source.isDirectory()) {
			if (!target.exists()) {
				target.mkdir();
			}
			String[] fileList;
			fileList = source.list();
			for (int i = 0; i < fileList.length; i++) {
				copyFileOrDir(sourceURL + "\\" + fileList[i], targetURL + "\\"
						+ fileList[i]);
			}
		}
	}

//	public void moveFileOrDir(String PId, String targetURL) {
//		PId="F:plan_annex/8e2690dc-4a57-4c25-83a1-47496c147c18.html";
//		targetURL="E:plan_annex/8e2690dc-4a57-4c25-83a1-47496c147c18.html";
//		copyFileOrDir( PId,targetURL);
//		PId="F:plan_annex/8e2690dc-4a57-4c25-83a1-47496c147c18.files";
//		targetURL="E:plan_annex/8e2690dc-4a57-4c25-83a1-47496c147c18.files";
////		File enclosureFile = new File(basePath.substring(0,basePath.lastIndexOf("/")));
////		if(!enclosureFile.exists()){
////			try {
////				enclosureFile.mkdir();
////			} catch (Exception e) {
////				// TODO Auto-generated catch block
////				e.printStackTrace();
////			}
////		}
//		copyFileOrDir( PId,targetURL);
//	}
}
